package com.lyfei.yd.admin.web;

import com.lyfei.yd.common.core.domain.AjaxResult;
import com.lyfei.yd.common.exception.CustomException;
import com.lyfei.yd.domain.dto.PageQueryDto;
import com.lyfei.yd.domain.dto.SpecKeyDto;
import com.lyfei.yd.domain.dto.SpecValueDto;
import com.lyfei.yd.domain.entity.SpecValue;
import com.lyfei.yd.service.SpecKeyService;
import com.lyfei.yd.service.SpecValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;

@RestController
@RequestMapping("/specs")
public class SpecsController {

    @Autowired
    private SpecKeyService specKeyService;

    @Autowired
    private SpecValueService specValueService;

    @GetMapping("/keys")
    public AjaxResult getSpecKeys(PageQueryDto dto) {
        return AjaxResult.success(specKeyService.listPage(dto));
    }

    @PostMapping("/addKey")
    public AjaxResult addSpecKey(@RequestBody SpecKeyDto dto) {
        specKeyService.create(dto);
        return AjaxResult.success();
    }

    @DeleteMapping("/deleteKey/{id}")
    public AjaxResult deleteSpecKey(@PathVariable Long id) {
        specKeyService.delete(id);
        return AjaxResult.success();
    }

    @PostMapping("/updateKey/{id}")
    public AjaxResult updateSpecKey(@RequestBody SpecKeyDto dto, @PathVariable Long id) {
        specKeyService.update(dto, id);
        return AjaxResult.success();
    }

    @GetMapping("/keyDetail/{id}")
    public AjaxResult getSpecKeyDetail(@PathVariable Long id) {
        return AjaxResult.success(specKeyService.getSpecKeyAndItems(id));
    }

    @GetMapping("/key/list")
    public AjaxResult getSpecKeyList() {
        return AjaxResult.success(this.specKeyService.list());
    }

    @PostMapping("/addValue")
    public AjaxResult addSpecValue(@RequestBody SpecValueDto dto) {
        specValueService.create(dto);
        return AjaxResult.success();
    }

    @PostMapping("/updateValue/{id}")
    public AjaxResult updateSpecValue(@RequestBody SpecValueDto dto, @PathVariable Long id) {
        specValueService.update(dto, id);
        return AjaxResult.success();
    }

    @DeleteMapping("/deleteValue/{id}")
    public AjaxResult deleteSpecValue(@PathVariable Long id) {
        specValueService.delete(id);
        return AjaxResult.success();
    }

    @GetMapping("/value/{id}")
    public AjaxResult getSpecValue(@PathVariable Long id) {
        SpecValue specValue = specValueService.getById(id);
        if(specValue == null) {
            throw new CustomException("规格值不存在");
        }
        return AjaxResult.success(specValue);
    }

    @GetMapping("/by/spu/{id}")
    public AjaxResult getBySpuId(@PathVariable(value = "id") @Positive Long spuId) {
        return AjaxResult.success(specKeyService.getBySpuId(spuId));
    }

    @GetMapping("/value/by/key/{id}")
    public AjaxResult getBySpecKeyId(@PathVariable(value = "id") @Positive Long specKeyId) {
        return AjaxResult.success(specValueService.getByKeyId(specKeyId));
    }
}
